package com.weng.coder.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.coder.common.utils.Constants;
import com.weng.coder.entity.CodeTable;
import com.weng.coder.entity.NameRule;
import com.weng.coder.mapper.CodeTableMapper;
import com.weng.coder.mapper.NameRuleMapper;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 命名规则 业务实现类
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-25 16:03:09
 */
@Service("nameRuleService")
public class NameRuleServiceImpl  implements NameRuleService  {
	@Resource
	private NameRuleMapper nameRuleDao;
	@Resource
	private CodeTableMapper codeTableDao;

	public List<NameRule> findByIds(List<String> ids){
		return nameRuleDao.selectBatchIds(ids);
	}
	/**
	 * Save
	 * @param t
	 */
	@Transactional
	@Override
	public NameRule save(NameRule t) {
			if(StringUtils.isEmpty(t.getId())){
					nameRuleDao.insert(t);
			}else{
					nameRuleDao.updateById(t);
			}
		 return t;
	}

	@Override
	public void delete(String id) {
		nameRuleDao.deleteById(id);
	}

	@Override
	public IPage<NameRule> getDataByPage(Integer pageNo, Integer pageSize, NameRule t) {

		QueryWrapper<NameRule> qw = new QueryWrapper<>();
		if(!StringUtils.isEmpty(t.getProjectId()))  qw.eq("PROJECT_ID", t.getProjectId());
		if(!StringUtils.isEmpty(t.getRule()))  qw.like("RULE", "%"+ t.getRule()+"%");
		if(!StringUtils.isEmpty(t.getExpression()))  qw.like("EXPRESSION", "%"+ t.getExpression()+"%");
		IPage<NameRule> page = new PageModel<>(pageNo,pageSize);
		return this.nameRuleDao.selectPage(page,qw);
	}



	@Override
	public void delete(NameRule t) {
		nameRuleDao.deleteById(t.getId());
	}
	

	@Override
	public NameRule getOne(String id) {
		
		return nameRuleDao.selectById(id);
	}

	@Override
	public boolean exists(String id) {
		NameRule nameRule = this.nameRuleDao.selectById(id);
		if(nameRule != null){
			return true;
		}
		return false;
	}


	@Override
	public void deleteByIds(String[] ids) {
		if(ids!=null && ids.length>0){
			for(String id:ids){
				nameRuleDao.deleteById(id);
			}
		}
	}
		private List<NameRule> findByProjectId(String projectId){
			QueryWrapper<NameRule> qw = new QueryWrapper<>();
			qw.eq("PROJECT_ID", projectId);
			return this.nameRuleDao.selectList(qw);
		}
	 @Override
	 public String getDomainName(String projectId, String tableId) {
			CodeTable table = codeTableDao.selectById(tableId);
			String code = table.getCode();

			List<NameRule> names = findByProjectId(projectId);
			for (NameRule name : names) {
					String rule = name.getRule();
					if (rule.equals(Constants.NAME_GLOBAL_REPLACE)) {
							code = code.replace(name.getExpression(), "");
					} else if (rule.equals(Constants.NAME_LEFT_REPLACE)) {
							code = code.replaceFirst(name.getExpression(), "");
					} else if (rule.equals(Constants.NAME_RIGHT_REPLACE)) {
							if (code.endsWith(name.getExpression())) {
									int index = code.lastIndexOf(name.getExpression());
									code = code.substring(0, index);
							}
					} else if (rule.equals(Constants.NAME_LEFT_APPEND)) {
							code = name.getExpression() + code;
					} else if (rule.equals(Constants.NAME_RIGHT_APPEND)) {
							code += name.getExpression();
					}
			}
			if (code.indexOf("_") > 0) {
					String[] codes = code.split("_", -1);
					StringBuilder builder = new StringBuilder();
					for (String tempCode : codes) {
							String prefix = tempCode.length() > 0 ? tempCode.substring(0, 1) : "";
							String suffix = tempCode.length() > 1 ? tempCode.substring(1) : "";
							builder.append(prefix.toUpperCase()).append(suffix.toLowerCase());
					}
					return builder.toString();
			} else {
					String prefix = code.length() > 0 ? code.substring(0, 1) : "";
					String suffix = code.length() > 1 ? code.substring(1) : "";
					return prefix.toUpperCase() + suffix.toLowerCase();
			}
 }
	 
	 public int deleteByProjectId(String projectId){
		 QueryWrapper<NameRule> qw = new QueryWrapper<>();
		 qw.eq("PROJECT_ID", projectId);
		 return this.nameRuleDao.delete(qw);
	 }
}
